/**
 * 
 */
package com.csaba.db.views;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

class ViewLabelProvider extends LabelProvider
{
	private final Map<ImageDescriptor, Image> images = new HashMap<ImageDescriptor, Image>();

	@Override
	public String getText( final Object obj )
	{
		if ( obj instanceof IUserDBTreeObject )
			return ( (IUserDBTreeObject) obj ).getName();
		return obj.toString();
	}

	@Override
	public Image getImage( final Object obj )
	{
		if ( obj instanceof IUserDBTreeObject )
		{
			final ImageDescriptor desc = ( (IUserDBTreeObject) obj ).getImageDescriptor();
			if ( images.containsKey(desc) )
				return images.get(desc);
			final Image image = desc.createImage();
			images.put(desc, image);
			return image;
		}
		return null;
	}

	@Override
	public void dispose()
	{
		for ( final Image image : images.values() )
		{
			image.dispose();
		}
		images.clear();

		super.dispose();
	}
}